package com.example.noobpackage.ui.orderdetail

import android.os.Bundle
import android.transition.TransitionInflater
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.ExpressData
import com.example.noobpackage.tool.ExpressIconUtil
import com.example.noobpackage.ui.MainViewModel
import kotlinx.android.synthetic.main.fragment_order_detail.*


class OrderDetailFragment : Fragment() {

    private val args: OrderDetailFragmentArgs by navArgs()
    private val shareViewModel: MainViewModel by activityViewModels()
    private val viewModel: OrderDetailViewModel by viewModels()

    private lateinit var express: ExpressData

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition =
            TransitionInflater.from(context).inflateTransition(android.R.transition.move)
                .setDuration(400)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_order_detail, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 配置对应的快递信息
        Log.d("test", args.position.toString())
        express = shareViewModel.getExpress(args.position)!!

        // 配置不同的transitionName对应列表项
        typeImage.transitionName = "typeImage${args.position}"
        accessNumber.transitionName = "takingCode${args.position}"
        location.transitionName = "takingPlace${args.position}"

        // 配置对应显示信息
        Log.d("test", express.toString())
        typeImage.setImageResource(ExpressIconUtil.getIconResource(express.expressType))
        accessNumber.text = express.expressTakingNumber
        location.text = express.expressLocation
        packageNumber.text = express.expressNumber

        // 配置不同订单类型不同的显示
        if (express.isAccepted) {
            // 配置代取用户的信息
            viewModel.getProxyExpressContact(express.marketId!!).observe(viewLifecycleOwner) {
                phoneNumber.text = it.telephone
                helperName.text = it.name
                infoLoading.visibility = View.INVISIBLE
            }
        } else {
            infoLoading.visibility = View.INVISIBLE
            finishOrder.visibility = View.INVISIBLE
            undoOrder.visibility = View.VISIBLE

            phoneNumber.visibility = View.INVISIBLE
            PhoneNumberImage.visibility = View.INVISIBLE
            helperName.visibility = View.INVISIBLE
            HelperNameImage.visibility = View.INVISIBLE

            noAcceptedHint.visibility = View.VISIBLE
        }
    }
}